﻿using QC;
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows;


namespace SchedulesClassLibrary
{
    public class MachineSchedule:ISchedule
    {
        public string MaterialName { get; set; }
        public int MachineID { get; set; }
        public int QCAttribID { get; set; }
        public string QCAttribName { get; set; }
        public string Hour { get; set; }
        public string Day { get; set; }
        public DateTime TodaysDate { get; set; }

        //public int GroupID { get; set; }

        public string Value { get; set; }
        public decimal Target { get; set; }
        public decimal Threshold { get; set; }
        public decimal Upper { get; set; }
        public decimal Lower { get; set; }
        public bool Pass_Fail { get; set; }
        public DateTime DateTaken { get; set; }


        public List<MachineSchedule> GetMachinesRunning()
        {
            String connectionString = @"Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True";

            DataTable dt = new DataTable("data");
            var conn = new SqlConnection(connectionString);
            var cmd = new SqlCommand("CurrentMachinesData", conn);
            using (var da = new SqlDataAdapter(cmd))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                da.Fill(dt);
            }


            List<MachineSchedule> scheduleData = new List<MachineSchedule>();
            foreach (DataRow dr in dt.Rows)
            {
                scheduleData.Add(new MachineSchedule
                {
                    MachineID = (int)dr["MachineID"],
                    MaterialName = dr["MaterialName"].ToString(),
                    Pass_Fail = (bool)dr["Pass_Fail"],
                    DateTaken = (DateTime)dr["DateTaken"]
                    
 
                });
            }
            return scheduleData;
        }


        public List<MachineSchedule> GetCurrentShift()
        {
            String connectionString = @"Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True";

            DataTable dt = new DataTable("data");
            using (var conn = new SqlConnection(connectionString))
            using (var cmd = new SqlCommand("MyShiftMachnData", conn))
            using (var da = new SqlDataAdapter(cmd))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                da.Fill(dt);
            }

            List<MachineSchedule> teamStatusData = new List<MachineSchedule>();
            foreach (DataRow dr in dt.Rows)
            {
                teamStatusData.Add(new MachineSchedule
                {
                    MachineID = (int)dr["MachineID"],
                    MaterialName = dr["MaterialName"].ToString(),
                    QCAttribName = dr["QCAttribName"].ToString(),
                    Hour = dr["Hour"].ToString(),
                    Day = dr["Day"].ToString(),
                });
            }
            return teamStatusData;
        }


        public List<MachineSchedule> GetAllShifts()
        {
            String connectionString = @"Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True"; //CPCC --> 
            //String connectionString = @"Data Source=MYLAPTOP;Initial Catalog=QCConfigurationManager;Integrated Security=True";
            //TEST string connectionString = ConfigurationManager.ConnectionStrings["MyLaptopConnection"].ToString();

            DataTable dt = new DataTable("data");
            var conn = new SqlConnection(connectionString);
            var cmd = new SqlCommand("AllShiftsMachnData", conn);
            using (var da = new SqlDataAdapter(cmd))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                da.Fill(dt);
            }

            List<MachineSchedule> machinesDatagrid = new List<MachineSchedule>();
            foreach (DataRow dr in dt.Rows)
            {
                machinesDatagrid.Add(new MachineSchedule
                {
                    MachineID = (int)dr["MachineID"],
                    MaterialName = dr["MaterialName"].ToString(),
                    QCAttribName = dr["QCAttribName"].ToString(),
                    Hour = dr["Hour"].ToString(),
                    Day = dr["Day"].ToString(),
                });
            }
            return machinesDatagrid;
        }


        public List<MachineSchedule> GetTeamScheduleData(int groupID)
        {
            throw new NotImplementedException();
        }

        public List<MachineSchedule> SingleMachineRunning(int MachnID)
        {
            String connectionString = @"Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True";

            DataTable dt = new DataTable("data");
            var conn = new SqlConnection(connectionString);
            var cmd = new SqlCommand("SingleMachinesData", conn);
            using (var da = new SqlDataAdapter(cmd))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@MachineID", MachnID);
                da.Fill(dt);
            }

            List<MachineSchedule> singleMachine = new List<MachineSchedule>();
            foreach (DataRow dr in dt.Rows)
            {
                singleMachine.Add(new MachineSchedule
                {
                    MachineID = (int)dr["MachineID"],
                    MaterialName = dr["MaterialName"].ToString(),
                    QCAttribID = (int)dr["QCAttribID"],
                    Value = dr["Value"].ToString(),
                    Target = (decimal)dr["Target"],
                    Threshold = (decimal)dr["Threshold"],
                    Upper = (decimal)dr["Upper"],
                    Lower = (decimal)dr["Lower"]

                });
            }
            return singleMachine;
        }



    }

}
